package com.ljc.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljc.dao.DevicePurchaseDemandDao;
import com.ljc.dto.*;
import com.ljc.service.DeviceService;
import com.ljc.util.JWTUtil;
import com.ljc.vo.Count;
import com.ljc.vo.DeviceSearch;
import com.ljc.vo.StaOrg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("device")
@Slf4j
@CrossOrigin
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @RequestMapping("searchByAdmit")
    public Result searchByAdmit(HttpServletRequest request, @RequestBody  DeviceSearch deviceSearch){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        deviceSearch.setAdmitId(Integer.valueOf(id));
        PageHelper.startPage(deviceSearch.getPageNum(),deviceSearch.getPageSize());
        List<Devices> devices = deviceService.searchByAdmit(deviceSearch);
        PageInfo<Devices> devicesPageInfo = new PageInfo<>(devices);
        result.setStatus(1).setMsg("获取设备信息成功").setData(devicesPageInfo);
        log.info("[{}]",deviceSearch);
        return result;
    }
    @RequestMapping("statTotalByAdmit")
    public Result statTotalByAdmit(HttpServletRequest request,@RequestBody DeviceSearch deviceSearch){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        deviceSearch.setAdmitId(Integer.valueOf(id));
        List<DeviceDistribute> deviceDistributes = deviceService.statTotalByAdmit(deviceSearch);
        result.setStatus(1).setMsg("统计成功").setData(deviceDistributes);
        return  result;

    }
    @RequestMapping("getAllName")
    public Result getAllName(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        List<String> allNameByAdmit = this.deviceService.getAllNameByAdmit(Integer.valueOf(id));
        result.setMsg("获取所有设备成功").setStatus(1).setData(allNameByAdmit);
        return result;
    }

    @RequestMapping("count")
    public Result count(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        Count count = deviceService.count(Integer.valueOf(id));
        result.setData(count).setMsg("获取总数统计").setStatus(1);
        return result;
    }

    @RequestMapping("getAllOrg")
    public Result getAllOrg(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        List<String> allOrganization = deviceService.getAllOrganization(Integer.valueOf(id));
        result.setStatus(1).setMsg("获取所有学院成功").setData(allOrganization);
        return result;
    }

    @RequestMapping("statAgeByAdmit")
    public Result statAgeByAdmit(HttpServletRequest request,@RequestBody DeviceSearch deviceSearch){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        deviceSearch.setAdmitId(Integer.valueOf(id));
        List<DeviceStatAge> deviceStatAges = deviceService.statAgeByAdmit(deviceSearch);
        result.setStatus(1).setMsg("统计设备购置年限").setData(deviceStatAges);
        return  result;
    }
    @RequestMapping("searchByUser")
    public Result searchByUser(HttpServletRequest request ,@RequestBody DeviceSearch deviceSearch){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        deviceSearch.setUserId(Integer.valueOf(id));
        PageHelper.startPage(deviceSearch.getPageNum(),deviceSearch.getPageSize());
        List<Devices> devices = deviceService.searchByUser(deviceSearch);
        PageInfo<Devices> devicesPageInfo = new PageInfo<>(devices);
        result.setMsg("获取用户购置列表成功").setStatus(1).setData(devicesPageInfo);
        return result;
    }
    @RequestMapping("deviceSta")
    public Result deviceSta(HttpServletRequest request ,@RequestBody DeviceSearch deviceSearch){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        deviceSearch.setUserId(Integer.valueOf(id));
        List<DeviceSta> deviceStas = deviceService.deviceSta(deviceSearch);
        result.setMsg("按年份统计设备成功").setStatus(1).setData(deviceStas);
        return result;
    }
    @RequestMapping("countQuantityByUser")
    public Result countQuantityByUser(HttpServletRequest request  ){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        int i = deviceService.countQuantityByUser(Integer.valueOf(id));
        result.setMsg("统计用总设备数成功").setStatus(1).setData(i);
        return result;
    }
    @RequestMapping("countPriceByUser")
    public Result countPriceByUser(HttpServletRequest request  ){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        int i = deviceService.countPriceByUser(Integer.valueOf(id));
        result.setMsg("统计用总设备数成功").setStatus(1).setData(i);
        return result;
    }

    @RequestMapping("searchNameByUser")
    public Result searchNameByUser(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        List<String> strings = deviceService.searchNameByUser(Integer.valueOf(id));
        result.setStatus(1).setMsg("查找用户所有购购置设备名成功").setData(strings);
        return  result;
    }
    @RequestMapping("deviceStaAgeByUser")
    public Result deviceStaAgeByUser(HttpServletRequest request , @RequestBody DeviceSearch deviceSearch){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        deviceSearch.setUserId(Integer.valueOf(id));
        List<DeviceStatAge> deviceStatAges = deviceService.deviceAgeStaByUser(deviceSearch);
        result.setStatus(1).setMsg("按年份统计用户设备数量成功").setData(deviceStatAges);
        return result;
    }
    @RequestMapping("staByOrg")
    public Result staByOrg(HttpServletRequest request , @RequestBody StaOrg staOrg){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        DecodedJWT decodedJWT = JWTUtil.getTokenInfo(token);
        String id = decodedJWT.getClaim("id").asString();
        staOrg.setDeanId(Integer.valueOf(id));
        List<DeviceSta> devices=deviceService.staOrg(staOrg);
        result.setStatus(1).setMsg("统计单个学院数据成功").setData(devices);
        return result;
    }
}
